<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use common\models\Category;

/**
 * This is the model class for table "{{%coach}}".
 *
 * @property integer $id
 * @property string $name
 * @property integer $pid
 * @property string $thumb
 * @property integer $sort
 * @property string $content
 * @property integer $created_at
 * @property integer $updated_at
 */
class Coach extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%coach}}';
    }

    // 行为
    public function behaviors()
    {
        return [
          [ 'class' => TimestampBehavior::className()]
        ];
    }

    // 获取栏目
    public function getCategory()
    {
        return $this->hasOne(Category::className(), ['id' => 'pid']);
    }

    // 获取所有栏目
    public function getAllCategory()
    {
        return Category::find()->where(['pid' => 2])->all();
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'pid', 'thumb'], 'required'],
            [['pid', 'sort'], 'integer'],
            [['content', 'thumb'], 'string'],
            [['name'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '姓名',
            'pid' => '分类',
            'thumb' => '头像',
            'sort' => '排序',
            'content' => '摘要',
            'created_at' => '添加时间',
            'updated_at' => '更新时间',
        ];
    }
}
